package com.example.mynotepad;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.app.AlertDialog;
import android.app.backup.RestoreObserver;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mynotepad.adaper.NotepadAdapter;
import com.example.mynotepad.bean.RecordBean;
import com.example.mynotepad.database.MyOpenHelper;
import com.example.mynotepad.utils.MyConstant;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int SEND_CORD = 1111;
    private ListView mListview;
    private ImageView mAdd;
    private MyOpenHelper myOpenHelper;
    private List<RecordBean> recordBeanList;
    private NotepadAdapter notepadAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.flagColor));

        setContentView(R.layout.activity_main);
        initView();
        // 显示数据库中记录到列表控件
        myOpenHelper = new MyOpenHelper(this);
        showData();

        // 添加记录按钮 跳转到RecordActivity
        mAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                startActivityForResult(intent, SEND_CORD);
            }
        });
    }

    private void showData() {
        // 显示数据库中记录到列表控件
        if (recordBeanList != null){
            recordBeanList.clear();
        }
        recordBeanList = myOpenHelper.queryRecord(); // 查询数据
        notepadAdapter = new NotepadAdapter(this, recordBeanList);
        mListview.setAdapter(notepadAdapter);

        // 点击列表条目，进入RecordActivity界面的编辑状态
        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                RecordBean recordBean = recordBeanList.get(i);  // i是被点击条目的编号
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                intent.putExtra("id", recordBean.getId());
                intent.putExtra("record", recordBean.getRecord());
                intent.putExtra("time", recordBean.getTime());
                startActivityForResult(intent, SEND_CORD);
            }
        });

        // 长按条目，弹对话框，删除记录
        mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> adapterView, View view, final int position, long l) {
                AlertDialog alertDialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("删除记录吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                RecordBean recordBean = recordBeanList.get(position);
                                // 从数据表中删除长按的那条记录
                                int n = myOpenHelper.deleteRecord(recordBean.getId());
                                if (n > 0){
                                    // 从集合中移除长按的那条记录
                                    recordBeanList.remove(position);
                                    // 刷新适配器
                                    notepadAdapter.notifyDataSetChanged();
                                    Toast.makeText(MainActivity.this, "删除成功",
                                            Toast.LENGTH_SHORT).show();
                                    dialogInterface.dismiss();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        });
                alertDialog = builder.create();   // 初始化对话框对象
                alertDialog.show();  // 对话框显示
                return true;  // 返回true 实现了长按事件
            }
        });

    }

    // 从RecordActivity界面返回后，刷新列表，显示数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SEND_CORD && resultCode == RESULT_OK){
            // 查询保存在数据库中的全部数据
            showData();
        }
    }

    private void initView() {
        mListview = (ListView) findViewById(R.id.listview);
        mAdd = (ImageView) findViewById(R.id.add);
    }
}
